<template>
  <div class="container">
    <el-dialog
      title="修改银行卡信息"
      :visible.sync="dialog.centerDialogVisible"
      width="500px"
      center
      :lock-scroll="true"
      :before-close="hideDialog"
      @open="getData"
      @close="hideDialog"
    >
      <el-form
        ref="form"
        :model="formData"
        label-width="150px"
        label-position="left"
        :rules="rules"
      >
        <el-form-item label="开户账号姓名：" prop="bank_acount_person_name">
          <el-input
            style="width: 200px"
            v-model="formData.bank_acount_person_name"
            placeholder="请输入开户账号姓名"
          />
        </el-form-item>
        <el-form-item label="开户行：" prop="bank">
          <el-input
            style="width: 200px"
            v-model="formData.bank"
            placeholder="请输入开户行"
          />
        </el-form-item>
        <el-form-item label="开户账号：" prop="bank_acount">
          <el-input
            style="width: 200px"
            :disabled="true"
            v-model="formData.bank_acount"
            placeholder="请输入开户账号"
          />
        </el-form-item>
        <el-form-item label="行号 ：">
          <el-input
            style="width: 200px"
            v-model="formData.bank_code"
            placeholder="请输入行号"
          />
        </el-form-item>
        <el-form-item label="账户类型：" prop="bank_type">
          <el-select v-model="formData.bank_type" placeholder="请选择账户类型">
            <el-option
              v-for="item in bank_typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开户行省份：">
          <el-select
            clearable
            v-model="formData.bank_province"
            :multiple="false"
            filterable
            remote
            reserve-keyword
            placeholder="请输入开户行省份"
            :remote-method="remoteMethod"
            :loading="bank_province.loading"
          >
            <el-option
              v-for="item in bank_province.options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="银行：">
          <el-select
            clearable
            v-model="formData.main_bank"
            :multiple="false"
            filterable
            remote
            reserve-keyword
            placeholder="请输入银行"
            :remote-method="remoteMethods"
            :loading="main_bank.loading"
          >
            <el-option
              v-for="item in main_bank.options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  selectBank,
  updateBanckData,
  selectBankProvince,
} from "@/api/supplier/supplierList";
export default {
  props: {
    dialog: Object,
  },
  data() {
    return {
      formData: {
        bank_acount_person_name: "",
        bank: "",
        bank_acount: "",
        bank_type: "",
        bank_province: "",
        main_bank: "",
      },
      rules: {
        bank_acount_person_name: [
          { required: true, message: "填写开户账号姓名", trigger: "blur" },
        ],
        bank_acount: [
          { required: true, message: "填写开户账号", trigger: "blur" },
        ],
        bank: [{ required: true, message: "填写开户行", trigger: "blur" }],
        bank_type: [
          { required: true, message: "选择账户类型", trigger: "change" },
        ],
      },
      bank_typeList: [
        {
          value: 1,
          label: "私户",
        },
        {
          value: 2,
          label: "公户",
        },
      ],
      main_bank: {
        options: [],
        value: "",
        list: [],
        loading: false,
      },
      bank_province: {
        options: [],
        value: "",
        list: [],
        loading: false,
      },
    };
  },
  methods: {
    getData() {
      console.log(this.dialog.formData, 9999);
      this.formData = {
        bank_acount_person_name: this.dialog.formData.bank_acount_person_name,
        bank: this.dialog.formData.bank,
        bank_acount: this.dialog.formData.bank_acount,
        bank_type: this.dialog.formData.bank_type,
        bank_province: this.dialog.formData.bank_province,
        main_bank: this.dialog.formData.main_bank,
        bank_code: this.dialog.formData.bank_code,
      };
      this.bank_provinceList();
      this.main_bankList();
    },
    hideDialog() {
      this.formData = {
        bank_acount_person_name: "",
        bank: "",
        bank_acount: "",
        bank_type: "",
        bank_province: "",
        main_bank: "",
      };
      this.main_bank = {
        options: [],
        value: "",
        list: [],
        loading: false,
      };
      this.bank_province = {
        options: [],
        value: "",
        list: [],
        loading: false,
      };
      this.dialog.centerDialogVisible = false;
    },
    bank_provinceList(key = "") {
      let data = {
        key,
      };
      // 获取开户行省份
      selectBankProvince(data).then((res) => {
        if (res.code == 0) {
          this.bank_province.options = res.data.map((item) => {
            return {
              value: `${item.name}`,
              label: `${item.name}`,
            };
          });
        }
      });
    },
    main_bankList(key = "") {
      let data = {
        key,
      };
      // 获取银行
      selectBank(data).then((res) => {
        if (res.code == 0) {
          this.main_bank.options = res.data.map((item) => {
            return {
              value: `${item.name}`,
              label: `${item.name}`,
            };
          });
        }
      });
    },
    //搜索开户行省份
    remoteMethod(query) {
      if (query !== "") {
        this.bank_provinceList(query);
      } else {
        this.bank_province.options = [];
      }
    },
    //搜索银行
    remoteMethods(query) {
      if (query !== "") {
        this.main_bankList(query);
      } else {
        this.main_bank.options = [];
      }
    },
    //提交
    confirm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = {
            id: this.dialog.id,
            bank_acount_person_name: this.formData.bank_acount_person_name,
            bank: this.formData.bank,
            bank_acount: this.formData.bank_acount,
            bank_type: this.formData.bank_type,
            bank_province: this.formData.bank_province,
            main_bank: this.formData.main_bank,
            bank_code:this.formData.bank_code,
          };
          updateBanckData(data)
            .then((res) => {
              if (res.code == 0) {
                this.$message({
                  message: res.message || res.msg,
                  type: "success",
                });
                this.hideDialog();
                this.$emit("upData");
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },

    //取消
    cancel() {
      this.hideDialog();
    },
  },
};
</script>
<style scoped>
.tab {
  display: flex;
}
.tab .el-form-item {
  flex: 1;
}
</style>